package tvsd;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import net.sf.torrentsniffer.torrent.Torrent;
import net.sf.torrentsniffer.torrent.TorrentImpl;
import net.sf.torrentsniffer.torrent.TorrentState;

import org.gnu.stealthp.rsslib.RSSChannel;
import org.gnu.stealthp.rsslib.RSSException;
import org.gnu.stealthp.rsslib.RSSHandler;
import org.gnu.stealthp.rsslib.RSSItem;
import org.gnu.stealthp.rsslib.RSSParser;

import common.TvShow;

public class TVSD {
	private static List<TvShow> shows = new ArrayList<TvShow>();

	public static void main(String[] args) throws MalformedURLException, RSSException {

		init();

		for (TvShow show : shows) {
			RSSHandler hand = new RSSHandler();
			// RSSParser.parseXmlFile(new
			// URL("http://www.showrss.karmorra.info/feeds/424.rss"), hand,
			// false);
			RSSParser.parseXmlFile(show.getUrl().getAuthority(), hand, false);

			RSSChannel ch = hand.getRSSChannel();
			System.out.println("****************CHANNEL INFO ************");
			System.out.println(ch.toString());

			if (ch.getRSSImage() != null) {
				System.out.println("IMAGE INFO: \n" + ch.getRSSImage().toString());
				System.out.println("IMAGE IN HTML: \n" + ch.getRSSImage().toHTML());

			} else
				System.out.println("CHANNEL HAS NO IMAGE");

			if (ch.getRSSTextInput() != null) {
				System.out.println("INPUT INFO: \n" + ch.getRSSTextInput().toString());
				System.out.println("HTML INPUT:\n" + ch.getRSSTextInput().toHTML());
			} else
				System.out.println("CHANNEL HAS NO FORM INPUT");

			if (ch.getDoublinCoreElements() != null) {
				System.out.println("DOUBLIN CORE INFO:");
				// Hashtable tbl = ch.getDoublinCoreElements();

				System.out.println(ch.getDoublinCoreElements().toString());
			} else
				System.out.println("CHANNEL HAS NO DOUBLIN CORE TAGS");

			System.out.println("SEQUENCE INFO:");
			if (ch.getItemsSequence() != null)
				System.out.println(hand.getRSSChannel().getItemsSequence().toString());
			else
				System.out.println("CHANNELL HAS NO SEQUENCE MYBE VERSION 0.9 or 2?");

			System.out.println("*****************************************");
			LinkedList lst = hand.getRSSChannel().getItems();
			System.out.println("****************ITEMS INFO *************");
			System.out.println("ITEM SIZE: " + lst.size());
			System.out.println("****************************************");

			for (int i = 0; i < lst.size(); i++) {
				RSSItem itm = (RSSItem) lst.get(i);
				System.out.println("****************ITEM DETAILS *************");
				System.out.println(itm.toString());

				Torrent torrent = new TorrentImpl(new URL(itm.getLink()));

				TorrentState state = torrent.getState();

				System.out.println("Complete " + state.getComplete());
				System.out.println("Downloaded " + state.getDownloaded());
				System.out.println("Incomplete " + state.getIncomplete());
				System.out.println("Name " + state.getName());

				if (itm.getDoublinCoreElements() != null) {
					System.out.println("DOUBLIN CORE INFO FOR ITEM:");
					System.out.println(itm.getDoublinCoreElements().toString());
				} else
					System.out.println("ITEM HAS NO DOUBLIN CORE TAGS");

				System.out.println("******************************************");
			}
		}
	}

	private static void init() throws MalformedURLException {
		TvShow show = new TvShow("Spartacus", new URL("http://sparta.xml"));
		shows.add(show);
		show = new TvShow("Alcatraz", new URL("http://alcatraz.xml"));
		shows.add(show);
	}
}
